package com.ruoyi.web.controller.mes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.domain.mes.MesOrderProduct;
import com.ruoyi.web.mapper.MesConfigMapper;
import com.ruoyi.web.service.DeliveryService;
import com.ruoyi.web.service.MesPaperOrderDetailService;
import com.ruoyi.web.service.MesPaperOrderService;
import com.ruoyi.web.vo.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/paperOrder")
public class MesPaperOrderController extends BaseController {
    @Resource
    private MesPaperOrderService mesPaperOrderService;
    @Resource
    private MesPaperOrderDetailService paperOrderDetailService;

    @Resource
    private MesConfigMapper configMapper;

    @GetMapping(value = "/getlist")
    public AjaxResult getlist(MesPaperOrder paperOrder) {
        startPage();
        List<MesPaperOrder> paperOrders = mesPaperOrderService
                .list(new QueryWrapper<MesPaperOrder>().lambda()
                .eq(StringUtils.isNotEmpty(paperOrder.getWorkshopId()), MesPaperOrder::getWorkshopId, paperOrder.getWorkshopId())
                .eq(StringUtils.isNotEmpty(paperOrder.getWorkshopName()), MesPaperOrder::getWorkshopName, paperOrder.getWorkshopName()));
        return AjaxResult.success("查询成功", paperOrders);
    }
    @GetMapping(value = "/getById/{id}")
    public AjaxResult getById(@PathVariable("id") String id) {
        MesPaperOrder paperOrder = mesPaperOrderService.getById(id);
        List<MesPaperOrderDetail> PaperOrderDetails = paperOrderDetailService
                .list(new QueryWrapper<MesPaperOrderDetail>()
                .lambda().eq(MesPaperOrderDetail::getPaperOrderId, paperOrder.getId()));
        paperOrder.setPaperOrderDetails(PaperOrderDetails);
        return AjaxResult.success("查询成功", paperOrder);
    }
    //根据条件查询加放比例
    @GetMapping(value = "/getcoefficient")
    public AjaxResult getcoefficient(String printType,String sheetSize,String workshopId, String printColour,Integer printCount) {
        String coefficient="0";
        List<MesPaperOrder> PaperOrders=new ArrayList<>();
        if (Objects.equals(printType, "1")){//平张
             PaperOrders = mesPaperOrderService
                    .list(new QueryWrapper<MesPaperOrder>()
                            .lambda().like(MesPaperOrder::getPrintColour, printColour));
        }else{//卷筒
             PaperOrders = mesPaperOrderService
                    .list(new QueryWrapper<MesPaperOrder>()
                            .lambda().eq(MesPaperOrder::getSheetSize, sheetSize)
                            .eq(MesPaperOrder::getWorkshopId, workshopId));
        }
        if(PaperOrders!=null&&PaperOrders.size()>0){
            String id= PaperOrders.get(0).getId();
            List<MesPaperOrderDetail> PaperOrderDetails = paperOrderDetailService
                    .list(new QueryWrapper<MesPaperOrderDetail>()
                            .lambda().eq(MesPaperOrderDetail::getPaperOrderId, id)
                            .le(MesPaperOrderDetail::getPrintCount, printCount)
                            .orderByDesc(MesPaperOrderDetail::getPrintCount));
            if (PaperOrderDetails!=null&& PaperOrderDetails.size()>0){
                coefficient=PaperOrderDetails.get(0).getLossCoefficient();
            }
        }
        return AjaxResult.success("查询成功", coefficient);
    }
    @PostMapping(value = "/add")
    @Transactional
    public AjaxResult add(@RequestBody MesPaperOrder paperOrder) {
        if (paperOrder.getId() == null) {
            String id = UUID.randomUUID().toString().replace("-", "");

            Map<String, Object> map = new HashMap<>();
            map.put("columnName", "order_num");
            map.put("tableName", "mes_paper_order");
            int maxNum = configMapper.getMaxId(map);

            String code = PinYinUtils.getCodeNormal(maxNum, "XDBZ");
            paperOrder.setOrderNum(code);
            paperOrder.setId(id);
            paperOrder.setOrderNum(code);
            paperOrder.setCreateTime(DateUtils.getNowDate());

            mesPaperOrderService.saveOrUpdate(paperOrder);
            if (paperOrder.getPaperOrderDetails() != null) {
                for (MesPaperOrderDetail paperOrderDetail : paperOrder.getPaperOrderDetails()) {
                    paperOrderDetail.setPaperOrderId(id);
                }
            }
                paperOrderDetailService.saveBatch(paperOrder.getPaperOrderDetails());
            } else {
                mesPaperOrderService.saveOrUpdate(paperOrder);
                paperOrderDetailService.remove(new QueryWrapper<MesPaperOrderDetail>().lambda()
                        .eq(MesPaperOrderDetail::getPaperOrderId, paperOrder.getId()));
                paperOrderDetailService.saveBatch(paperOrder.getPaperOrderDetails());
            }
            return AjaxResult.success("操作成功", paperOrder);
        }
    @PostMapping(value = "/deleteById")
    public AjaxResult deleteById(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        mesPaperOrderService.removeById(id);
        paperOrderDetailService.remove(new QueryWrapper<MesPaperOrderDetail>()
                .lambda().eq(MesPaperOrderDetail::getPaperOrderId, id));
        return AjaxResult.success("删除成功");
    }

}

